#!/bin/bash

if [ $# -lt 1 ]; then
  echo "USAGE: $0 release|debug"
  echo " e.g.: $0 debug"
  exit 1
fi

CPU_CORES=4

SCRIPT_PATH="$(
  cd "$(dirname "$0")"
  pwd -P
)"
echo $SCRIPT_PATH
cd $SCRIPT_PATH

#create dir if not exist
release_dir="${SCRIPT_PATH}/cmake-build-release"
debug_dir="${SCRIPT_PATH}/cmake-build-debug"
if [ ! -d ${release_dir} ];then
  mkdir -p ${release_dir}
fi

if [ ! -d ${debug_dir} ];then
  mkdir -p ${debug_dir}
fi

if [ $1 = "release" ]; then
  cd ${release_dir}
  cmake -DCMAKE_BUILD_TYPE=Release .. && cmake --build . -- -j $CPU_CORES
elif [ $1 = "debug" ]; then
  cd ${debug_dir}
  cmake -DCMAKE_BUILD_TYPE=Debug .. && cmake --build . -- -j $CPU_CORES
else
  echo "error build type"
fi
